package com.example.demo.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.demo.impl.DahuaVideoService;
import com.example.demo.utils.SignVideoUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.security.NoSuchAlgorithmException;

/**
 * @author xumf
 * @date 2019/11/25 16:08
 */
@Controller
public class IndexController {

    @Autowired
    private DahuaVideoService dahuaVideoService;

    /**
     * 大华视频对接
     * @return
     * @throws NoSuchAlgorithmException
     */
    @RequestMapping("getVideoToken")
    @ResponseBody
    public String dahuaVideo() throws NoSuchAlgorithmException {
        String user_value="system";
        String pass_value="zsxxzx2280191";
        String realm_value = "TheNextService";
        String result=dahuaVideoService.sendFirstAuthorize();
        if(result!=null){
            JSONObject object=JSON.parseObject(result);
            String randomKey= object.getString("randomKey");
            System.out.println("randomKey"+randomKey);
            String sign= SignVideoUtils.videoSign(realm_value,randomKey,user_value,pass_value);
            System.out.println("sign="+sign);
            String result2=dahuaVideoService.sendSecondAuthorize(sign,randomKey);
            if(result2!=null){
                JSONObject object1=JSON.parseObject(result2);
                String token= object1.getString("token");
                System.out.println(token);
                return token;
            }
        }
        return null;
    }
}
